package jalus.blog;

import jalus.FMConfigureFactory;
import jalus.Action;
import jalus.service.ConfigureService;
import jalus.service.PostService;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import freemarker.template.Template;
import freemarker.template.TemplateException;

public class SitemapServlet extends Action {
	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		PostService postService = PostService.getInstance();
		List<jalus.pojo.Post> postList = postService.getAllPostList();

		Template template = FMConfigureFactory.getFMConfigure().getTemplate("tool/sitemap.xml");
				
		Map root = new HashMap();
		root.put("postlist", postList);
		root.put("config", ConfigureService.getInstance());
		
        resp.setCharacterEncoding("utf-8"); 
        resp.setContentType("text/xml; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        try {
        	template.process(root, out);
		} catch (TemplateException e) {
			e.printStackTrace();
		}
        out.flush();
	}
}
